package com.Drools.CIPS.ruler;

import com.Drools.CIPS.util.MapIN;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class JS_Ruler {

    public static boolean excuteJava(String classPath, String methodName, Map<String, Object> msgMap){

        try {
            //读取java文件
            Class<?> aClass = Class.forName(classPath);

            // 获取方法参数类型
            Class<?>[] parameterTypes = {Map.class};

            //调用指定方法
            Method method = aClass.getMethod(methodName, parameterTypes);

            Object obj = aClass.getDeclaredConstructor().newInstance();

            //执行方法
            boolean invoke = (boolean) method.invoke(obj,msgMap);

            return invoke;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }
    public static boolean excuteJs(String classPath, String methodName, Map<String,Object> msg){

        if(true) return false;

        boolean res = false;

        //取得脚本引擎的管理器Manager
        ScriptEngineManager scriptManager= new ScriptEngineManager();

        //根据名称取得js的脚本引擎
        ScriptEngine engine = scriptManager.getEngineByName("javascript");

        try {
            //读js文件
            FileReader fileReader = new FileReader(classPath);

            //执行文件
            engine.eval(fileReader);

            if(engine instanceof Invocable){

                Invocable invoke = (Invocable)engine;

                //执行校验方法
                boolean result= (boolean) invoke.invokeFunction(methodName, msg);

                return result;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;

    }

    public boolean method1(Map<String,Object> msgMap){
//        Object o = MapIN.GetValue(msgMap, "Document.FIToFICstmrCdtTrf.GrpHdr.MsgId");
//        if(o==null){
//            o="0";
//        }
//        return o.toString().equals("400");
        return false;

    }
}
